package lexer

// TokenType 是一个字符串，用于标识不同类型的token
type TokenType string

// Token 结构体表示词法单元
type Token struct {
	Type    TokenType
	Literal string
	Line    int // 行号
	Column  int // 列号
}

// 定义所有token类型常量
const (
	ILLEGAL = "ILLEGAL" // 非法字符
	EOF     = "EOF"     // 文件结束

	// 标识符和字面量
	IDENT  = "IDENT"  // 标识符, 如 add, x, y 等
	INT    = "INT"    // 整数值
	STRING = "STRING" // 字符串
	FLOAT  = "FLOAT"  // 浮点数

	// 模板字符串相关
	TEMPLATE = "TEMPLATE" // 模板字符串

	// 运算符
	ASSIGN   = "="
	PLUS     = "+"
	MINUS    = "-"
	BANG     = "!"
	ASTERISK = "*"
	SLASH    = "/"
	PERCENT  = "%"

	// 新增自增自减运算符
	INCREMENT = "++"
	DECREMENT = "--"

	// 新增复合赋值运算符
	PLUS_ASSIGN  = "+="
	MINUS_ASSIGN = "-="
	MUL_ASSIGN   = "*="
	DIV_ASSIGN   = "/="
	MOD_ASSIGN   = "%="

	LT  = "<"
	GT  = ">"
	LTE = "<="
	GTE = ">="

	EQ     = "=="
	NOT_EQ = "!="

	AND = "&&"
	OR  = "||"

	// 分隔符
	COMMA     = ","
	SEMICOLON = ";"
	COLON     = ":"
	DOT       = "."
	QUESTION  = "?"
	ARROW     = "=>" // 箭头函数

	LPAREN   = "("
	RPAREN   = ")"
	LBRACE   = "{"
	RBRACE   = "}"
	LBRACKET = "["
	RBRACKET = "]"

	// 关键字
	FUNCTION   = "FUNCTION"
	LET        = "LET"
	VAR        = "VAR"
	CONST      = "CONST"
	TRUE       = "TRUE"
	FALSE      = "FALSE"
	IF         = "IF"
	ELSE       = "ELSE"
	RETURN     = "RETURN"
	WHILE      = "WHILE"
	BREAK      = "BREAK"
	CONTINUE   = "CONTINUE"
	NULL       = "NULL"
	UNDEFINED  = "UNDEFINED"
	NEW        = "NEW"
	THIS       = "THIS"
	TYPEOF     = "TYPEOF"
	INSTANCEOF = "INSTANCEOF"
	DELETE     = "DELETE"
	VOID       = "VOID"
	IN         = "IN"
	TRY        = "TRY"
	CATCH      = "CATCH"
	FINALLY    = "FINALLY"
	THROW      = "THROW"
	SWITCH     = "SWITCH"
	CASE       = "CASE"
	DEFAULT    = "DEFAULT"
	WITH       = "WITH"
	FOR        = "FOR"
	OF         = "OF"

	// 新增类相关关键字
	CLASS       = "CLASS"
	EXTENDS     = "EXTENDS"
	CONSTRUCTOR = "CONSTRUCTOR"
	SUPER       = "SUPER"
	STATIC      = "STATIC"
)

// 关键字映射表
var keywords = map[string]TokenType{
	"function":   FUNCTION,
	"let":        LET,
	"var":        VAR,
	"const":      CONST,
	"true":       TRUE,
	"false":      FALSE,
	"if":         IF,
	"else":       ELSE,
	"return":     RETURN,
	"while":      WHILE,
	"break":      BREAK,
	"continue":   CONTINUE,
	"null":       NULL,
	"undefined":  UNDEFINED,
	"new":        NEW,
	"this":       THIS,
	"typeof":     TYPEOF,
	"instanceof": INSTANCEOF,
	"delete":     DELETE,
	"void":       VOID,
	"in":         IN,
	"of":         OF,
	"try":        TRY,
	"catch":      CATCH,
	"finally":    FINALLY,
	"throw":      THROW,
	"switch":     SWITCH,
	"case":       CASE,
	"default":    DEFAULT,
	"with":       WITH,
	"for":        FOR,

	// 新增类相关关键字
	"class":       CLASS,
	"extends":     EXTENDS,
	"constructor": CONSTRUCTOR,
	"super":       SUPER,
	"static":      STATIC,
}

// LookupIdent 检查给定的标识符是否为关键字
func LookupIdent(ident string) TokenType {
	if tok, ok := keywords[ident]; ok {
		return tok
	}
	return IDENT
}
